package com.zhao.dota.processor;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.zhao.dota.common.processor.SteamProcessor;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Request;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class AbilityProcessor extends SteamProcessor {

    private static final String URL = "https://api.stratz.com/api/v1/Ability?languageId=20";
    // 	https://www.dota2.com/datafeed/herodata?language=schinese&hero_id=103 根据英雄id获取技能

    public List<Ability> getAbilityList() {
        Request request = new Request.Builder().url(URL).get().build();
        String result = super.sendRequest(request);
        Map<Integer, Ability> abilitydata = super.readValue(result, new TypeReference<Map<Integer, Ability>>() {
        });
        List<Ability> abilities = new ArrayList<>();
        for (Ability ability : abilitydata.values()) {
            if (ability.getName() == null || ability.getDisplayName() == null) {
                continue;
            }
            abilities.add(ability);
            if (ability.getName().startsWith("special_bonus_unique_ursa")) {
                System.out.println(JSON.toJSONString(ability));
            }
        }
        return abilities;
    }

    @Data
    public static class Ability {

        private Integer id;

        private String name;
        /**
         * 所属英雄
         */
        private String uri;

        private boolean isTalent;

        private Language language;

        public String getDisplayName() {
            if (language == null) {
                return null;
            }
            return language.displayName;
        }
    }

    @Data
    private static class Language {

        private String displayName;

        private String[] description;

        private String[] attributes;

    }

}
